from fastapi import FastAPI, Request
from core import Events
from routers import api_router
from tortoise import Tortoise
from fastapi.templating import Jinja2Templates
from fastapi.staticfiles import StaticFiles

app = FastAPI(title="数据分析系统")
app.mount("/static", StaticFiles(directory="./static"), name="static")
templates = Jinja2Templates(directory="templates")
app.include_router(api_router)

# 事件监听
app.add_event_handler("startup", Events.startup(app))
app.add_event_handler("shutdown", Events.stopping(app))


@app.get('/')
def main_page(request: Request):
    data = [
        {"task_id": 1, "task_name": "测试任务", "task_status": "成功", "task_result": "http://www.baidu.com"},
        {"task_id": 2, "task_name": "测试任务", "task_status": "成功", "task_result": "www.baidu.com"},
        {"task_id": 3, "task_name": "测试任务", "task_status": "成功", "task_result": "www.baidu.com"},
        {"task_id": 4, "task_name": "测试任务", "task_status": "成功", "task_result": "www.baidu.com"},
        {"task_id": 5, "task_name": "测试任务", "task_status": "成功", "task_result": "www.baidu.com"},
        {"task_id": 6, "task_name": "测试任务", "task_status": "成功", "task_result": "www.baidu.com"},
    ]
    return templates.TemplateResponse("index.html", {"request": request, "data": data})


if __name__ == '__main__':
    import uvicorn

    uvicorn.run('main:app', host='127.0.0.1', port=8888, reload=True)
